package licode.unisop.types.dp;

import org.springframework.util.Assert;

public class Password {

    // 密码长度不少于8位且至少包含大写字母、小写字母、数字和特殊符号中的四种
    public static final String passwordRule4 = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,16}$";
    // 密码长度8-16位且至少包含大写字母、小写字母、数字或特殊符号中的任意三种
    public static final String passwordRule3 = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,16}$";

    private final String password;

    public Password(String password) {
        if (null != password) {
            // 密码空检查
            Assert.notNull(password, "密码为空：" + password);
            // 密码长度检查
            Assert.isTrue(password.length() >= 8,
                    "密码长度至少8位，当前：" + password.length());
            // 密码规则检查
            // checkPasswordRule(password);
            this.password = password;
        } else {
            this.password = "";
        }
    }

    public String getPassword() {
        return password;
    }

    public boolean equals(String code) {
        return password.equals(code);
    }

    @Override
    public String toString() {
        return password;
    }

    private void checkPasswordRule(String password) {
        Assert.isTrue(password.matches(passwordRule3),
                "密码不符合规则，至少包含大写字母、小写字母、数字或特殊符号中的任意三种");
    }
}
